package com.xiaxiang.xiawan;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xiaxiang.xiawan.Effect.EffectFragment;
import com.xiaxiang.xiawan.FunPhoto.FunPhotoFragment;
import com.xiaxiang.xiawan.Home.HomeFragment;
import com.xiaxiang.xiawan.My.MyFragment;
import com.xx.xiawan.net.DGHttpClient;
import com.xx.xiawan.net.DGRequestBase;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private static String TAG = "MainActivity";
    private final String FRAGMENT_TAG[] = {"home", "shop", "message", "own"};
    private List<FragmentBase> mFragmentList;
    private FragmentBase mCurrentFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private int tabIndex = 0;
    private TextView TitleBarText;
    private List<View> mBottomButton;
    private XXMainApplication mMainApplication;
    private boolean mIsPressArBtn =false;
    public static final int[] BottomPanelSelectedImageID = {
            R.mipmap.tabbar_home,
            R.mipmap.tabbar_effect,
            R.mipmap.tabbar_video,
            R.mipmap.tabbar_user
    };

    public static final int[] BottomPanelUnselectedImageID  = {
            R.mipmap.tabbar_home1,
            R.mipmap.tabbar_effect1,
            R.mipmap.tabbar_video1,
            R.mipmap.tabbar_user1
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mMainApplication = (XXMainApplication)getApplication();
        Log.e("st","onCreate ");
    }

    private void initView() {
        initFragment();
        initButton();
    }

    private void initFragment() {
        mFragmentList = new ArrayList<FragmentBase>();
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        FragmentBase mHomeFragment = new HomeFragment();
        mCurrentFragment = mHomeFragment;
        mFragmentList.add(mHomeFragment);
        FragmentBase mShopFragment = new EffectFragment();
        mFragmentList.add(mShopFragment);
        FragmentBase mMessageFragment = new FunPhotoFragment();
        mFragmentList.add(mMessageFragment);
        FragmentBase mMyFragment = new MyFragment();
        mFragmentList.add(mMyFragment);
        if ( !mFragmentList.get(tabIndex).isAdded() ) {
            fragmentTransaction.add(R.id.fragment_content, mFragmentList.get(tabIndex), FRAGMENT_TAG[tabIndex] );
        } else {
            fragmentTransaction.show(mFragmentList.get(tabIndex));
        }
        fragmentTransaction.commit();
    }

    class ButtonClickListener implements View.OnClickListener {
        int index;
        public ButtonClickListener(int index) {
            this.index = index;
        }

        @Override
        public void onClick(View view) {
            switchFragment(tabIndex,index);
            Log.i(TAG,"tabIndex "+ tabIndex +" index"+ index);
            tabIndex = index;

        }
    }

    private void initButton() {
        mBottomButton = new ArrayList<View>();
        View btn =  findViewById(R.id.imgBottomHomePage);
        mBottomButton.add(btn);

        btn.setOnClickListener(new ButtonClickListener(0));
        btn = findViewById(R.id.imgBottomShop);
        mBottomButton.add(btn);
        btn.setOnClickListener(new ButtonClickListener(1));

        btn = findViewById(R.id.imgBottomMsg);
        mBottomButton.add(btn);
        btn.setOnClickListener(new ButtonClickListener(2));

        btn =  findViewById(R.id.imgBottomMy);
        mBottomButton.add(btn);
        btn.setOnClickListener(new ButtonClickListener(3));

        findViewById(R.id.btn_ar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mIsPressArBtn = true;
                if(isLogin()){
                    startArActivity();
                }else{
                    XXConfigData.startLoginActivity(MainActivity.this);
                }
            }
        });
    }

    private void switchFragment(int from, int to) {
        fragmentTransaction = fragmentManager.beginTransaction();
        Drawable img;
        Resources res = getResources();
        img = res.getDrawable(BottomPanelUnselectedImageID[from]);
        img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
        TextView fromText = (TextView) mBottomButton.get(from);
        fromText.setCompoundDrawables(null,  img, null, null);
        fromText.setTextColor(this.getResources().getColor(R.color.white));
        fragmentTransaction.hide(mFragmentList.get(from));

        TextView toText = (TextView) mBottomButton.get(to);
        img = res.getDrawable(BottomPanelSelectedImageID[to]);
        img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
        toText.setCompoundDrawables(null, img, null, null);
        toText.setTextColor(this.getResources().getColor(R.color.default_text));
        if ( !mFragmentList.get(to).isAdded() ) {
            fragmentTransaction.add(R.id.fragment_content, mFragmentList.get(to), FRAGMENT_TAG[to] );
        } else {
            fragmentTransaction.show(mFragmentList.get(to));
        }
        mCurrentFragment = mFragmentList.get(to);
        fragmentTransaction.commit();
    }

    public boolean isLogin(){
       return mMainApplication.getIsLogin();
    }

    public void setIsLogin(boolean is){
        mMainApplication.setIsLogin(is);
    }

    public void initUserInfo(){
        mMainApplication.initUserInfo();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == XXConfigData.LOGIN_REQUEST_CODE){
            if(mIsPressArBtn){
                if(resultCode == XXConfigData.LOGIN_RECODE_SUCCESS){
                    initUserInfo();
                    startArActivity();
                }
                mIsPressArBtn = false;
                return;
            }else{

            }
        }
        if(mCurrentFragment != null){
            mCurrentFragment.onActivityResult(requestCode,resultCode,data);
        }
    }

    private void startArActivity(){
        Intent intent = new Intent("com.xiaxiang.xiawan.ar.camera");
        intent.putExtra("arMode", 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
    }

    public void onDestroy(){
        super.onDestroy();
        DGRequestBase.quit(getApplicationContext());
    }
    
    public void onStart(){
        super.onStart();
    }

    public void onResume(){
        super.onResume();
        initUserInfo();
    }
}
